home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / glibc-1.09 / glibc-1 / glibc-1.09.1 / resolv / nsap_addr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-28  |  1.7 KB  |  98 lines

  1. #if defined(LIBC_SCCS) && !defined(lint)
  2. static char rcsid[] = "$Id: nsap_addr.c,v 1.1 1994/07/28 21:53:01 roland Exp $";
  3. #endif /* LIBC_SCCS and not lint */
  4.  
  5. #include <sys/param.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <arpa/nameser.h>
  9. #include <ctype.h>
  10. #include <resolv.h>
  11.  
  12. #include "../conf/portability.h"
  13.  
  14. #if !defined(isxdigit)    /* XXX - could be a function */
  15. static int
  16. isxdigit(c)
  17.     register int c;
  18. {
  19.     return ((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'F'));
  20. }
  21. #endif
  22.  
  23. static char
  24. xtob(c)
  25.     register int c;
  26. {
  27.     return (c - (((c >= '0') && (c <= '9')) ? '0' : '7'));
  28. }
  29.  
  30. u_int
  31. inet_nsap_addr(ascii, binary, maxlen)
  32.     const char *ascii;
  33.     u_char *binary;
  34.     int maxlen;
  35. {
  36.     register u_char c, nib;
  37.     u_char *start = binary;
  38.     u_int len = 0;
  39.  
  40.     while ((c = *ascii++) != '\0' && len < maxlen) {
  41.         if (c == '.' || c == '+' || c == '/')
  42.             continue;
  43.         if (!isascii(c))
  44.             return (0);
  45.         if (islower(c))
  46.             c = toupper(c);
  47.         if (isxdigit(c)) {
  48.             nib = xtob(c);
  49.             if (c = *ascii++) {
  50.                 c = toupper(c);
  51.                 if (isxdigit(c)) {
  52.                     *binary++ = (nib << 4) | xtob(c);
  53.                     len++;
  54.                 } else
  55.                     return (0);
  56.             }
  57.             else
  58.                 return (0);
  59.         }
  60.         else
  61.             return (0);
  62.     }
  63.     return (len);
  64. }
  65.  
  66. char *
  67. inet_nsap_ntoa(binlen, binary, ascii)
  68.     int binlen;
  69.     register const u_char *binary;
  70.     register char *ascii;
  71. {
  72.     register int nib;
  73.     int i;
  74.     static char tmpbuf[255*3];
  75.     char *start;
  76.  
  77.     if (ascii)
  78.         start = ascii;
  79.     else {
  80.         ascii = tmpbuf;
  81.         start = tmpbuf;
  82.     }
  83.  
  84.     if (binlen > 255)
  85.         binlen = 255;
  86.  
  87.     for (i = 0; i < binlen; i++) {
  88.         nib = *binary >> 4;
  89.         *ascii++ = nib + (nib < 10 ? '0' : '7');
  90.         nib = *binary++ & 0x0f;
  91.         *ascii++ = nib + (nib < 10 ? '0' : '7');
  92.         if (((i % 2) == 0 && (i + 1) < binlen))
  93.             *ascii++ = '.';
  94.     }
  95.     *ascii = '\0';
  96.     return (start);
  97. }
  98.